<template>
  <div class="hawkContent">
    <hawk-breadcrumb></hawk-breadcrumb>
     <el-row :gutter='20'>
      <el-col :span='4' style="display:flex;align-items: center;">
          <span style="width:50px">名称</span>
        <el-input placeholder='请输入菜单名称搜索' v-model='queryForm.query.name'></el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="hawkSearch" @click="init">搜索</el-button>
        <el-button plain @click="reSet">重置</el-button>
      </el-col>
      <!-- <el-col :span='4' style='float: right;text-align: right;padding-right: 30px'>
        <el-button type='hawkCreate' @click='editUser(0,0)'>添加</el-button>
      </el-col> -->
    </el-row>
     <hawk-table
      :table-data="tableData"
      :selection="false"
      :page="queryForm.page"
      :tree-table="{children: 'children', hasChildren: 'hasChildren'}"
      :options="[
        { key: 'name', label: '菜单名称' },
        { key: 'type', label: '类型' },
        { key: 'remark', label: '备注' }
      ]"
    >
      <template #icon="{ row }">
        <i :class="'icon iconfont ' + row.icon "></i>
      </template>
      <template #type="{ row }">
        <span>{{row.type == 1 ? '一级菜单':row.type == 2 ? '二级菜单' :row.type == '3' ? '三级菜单或按钮' : '按钮'}}</span>
      </template>
      <template #handle="{ row }">
        <span class="hawk-text-btn" v-if="row.level < 4" @click="addChild(row)">添加子菜单</span>
        <span class="hawk-text-divider" v-if="row.level < 4">|</span>
        <span class="hawk-text-btn" @click="editUser(row)">编辑</span>
        <span class="hawk-text-divider">|</span>
        <span class="hawk-text-btn" @click="delUser(row)">删除</span>
      </template>
    </hawk-table>
    <el-dialog
      :title='formTitle'
      :visible.sync='addDialogVisible'
      :close-on-click-modal='false'
      @close='addDialogClosed'
    >
      <!-- 内容主题区 -->
      <el-form
        :model='addForm'
        :rules='addFormRules'
         label-position="top"
        ref='addFormRef'
        label-width='100px'
      >
        <el-form-item label='菜单名称' prop='name'>
          <el-input v-model='addForm.name'></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="addForm.icon"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select clearable v-model="addForm.type">
            <el-option :value="1" label="一级菜单"></el-option>
            <el-option :value="2" label="二级菜单页面"></el-option>
            <el-option :value="3" label="三级菜单或按钮"></el-option>
            <el-option :value="4" label="按钮"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label='路由路径' prop='route' v-if="addForm.type !== 2">
          <el-input v-model='addForm.route'></el-input>
        </el-form-item>
        <el-form-item label='组件路径' prop='component' v-if="addForm.type == 1">
          <el-input v-model='addForm.component'></el-input>
        </el-form-item>
        <el-form-item label='排序' prop='sort' v-if="addForm.type !== 2">
          <el-input v-model='addForm.sort'></el-input>
        </el-form-item>
        <el-form-item label='权限标识' prop='permissionId' v-if="addForm.type !== 0">
          <el-input v-model='addForm.permissionId'></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
/**
 * @author 韩玉龙
 * @description 菜单管理的视图层
 * @api
 * @since 2021年11月3日18:03
 * @update 2021年11月3日18:04
 * @modifier 韩玉龙
 * @bug 无
 * @version v1.0.0
 */

import { getTree,doEdit,doAdd,doDelete } from '@/api/system/menu';
export default {
  //import引入的组件需要注入到对象中才能使用",
  data() {
    return {
      // 类型开关
      btnType: 0,
      formTitle: '新增目录',
      addDialogVisible: false,
      multipleSelection: [],
      addForm: {
        id:'',
        pid:0,
        name:'',
        icon:'',
        type:0,
        route:'',
        component:'',
        sort:'',
        permissionId:'',
        level:1
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
        ],
        type: [
          { required: true, message: '请输入类型', trigger: 'change' },
        ],
        route: [
          { required: true, message: '请输入路由路径', trigger: 'blur' },
        ],
        component: [
          { required: true, message: '请输入组件路径', trigger: 'blur' },
        ],
        permissionId: [
          { required: true, message: '请输入权限标识', trigger: 'blur' },
        ]
      },
      tableData: [],
      total: 0,
      queryForm: {
        query: {
          name: ''
        }
      }
    }
  },
  //方法集合",
  methods: {
    reSet() {
      this.queryForm = this.$options.data().queryForm
    },
    async init() {
      let res = await getTree(this.queryForm.query.name)
      console.log(res)
      this.tableData = res.data
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      console.log(val)
      this.queryForm.page.size = val
      this.init()
    },
    handleCurrentChange(val) {
      console.log(val, 'handleCurrentChange')
      this.queryForm.page.current = val
      // this.init()
    },
    save() {
      this.$refs.addFormRef.validate(async valid => {
        console.log(valid)
        if (!valid) return
        if (this.addForm.id) {
          await doEdit(this.addForm)
          this.$message.success("修改成功")
          this.addDialogClosed();
        } else {
          await doAdd(this.addForm)
          this.$message.success("新增成功")
          this.addDialogClosed();
        }
      })
    },
    delUser(row) {
      this.$confirm(`此操作将删除该${row.type}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then( async () => {
         let ret = await doDelete(row.id)
          
          if (ret.code == 20000) {

            let type = ''
            if (ret.data) {
              ret.message = '删除成功'
              type = 'success'
            } else {
              type = 'error'
            }

            this.$message({
              type: type,
              message: ret.message,
            })

            // console.log(this.multipleSelection.length);
            if (this.multipleSelection.length == this.tableData.length) {
              this.queryForm.page.current = this.queryForm.page.current - 1
              if (this.queryForm.page.current < 1) {
                this.queryForm.page.current = 1
              }
            }

            this.multipleSelection = []
            console.log('-------------init---');
            this.init()

          }



        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addChild(row){
      this.formTitle = '新增子菜单'
      this.addForm.pid = row.id;
      this.addForm.level = row.level + 1
      this.addDialogVisible = true
    },
    editUser(row,type) {
      console.log(row)
      if (type==0) {
        row !== 0 ?  this.formTitle = '新增' + row.type : this.formTitle    
      } else {
        this.addForm = Object.assign({},row)
      }
      this.addDialogVisible = true
    },
    addDialogClosed() {
      this.addForm = this.$options.data().addForm
      this.$refs.addFormRef.resetFields()
      this.addDialogVisible = false
      this.init()
    }
  },
  //生命周期 - 创建之前",数据模型未加载,方法未加载,html模板未加载
  beforeCreate() {},

  //生命周期 - 创建完成（可以访问当前this实例）",数据模型已加载，方法已加载,html模板已加载,html模板未渲染
  created() {
    this.init()
  },

  //生命周期 - 挂载完成（可以访问DOM元素）",html模板已渲染
  mounted() {}
}
</script>

<style lang='scss'>
.hawkContent {
  width: calc(100% - 40px);
  min-height: calc(100vh - 140px);
  background: #fff;
  padding: 20px;
  border-radius: 8px;

  .inputLabel {
    font-size: 14px;
    color: #333333;
  }
.point:hover{
  cursor: pointer;
}
  .bottom-btns {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    margin-top: 10px;

    &-left {
      display: flex;
      align-items: center;

      &-num {
        margin-right: 10px;
        color: #333333;
        font-size: 14px;
      }
    }
  }
}
</style>